package com.itheima.web.client;

import com.itheima.model.domain.BusUser;
import com.itheima.service.impl.BusUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/bus")
public class BusUserController {
    @Autowired
    private BusUserService busUserService;
    @GetMapping("/findAll")
    public List<BusUser> findAll(){
        return busUserService.findAll();
    }
    @RequestMapping("/loginConfirm")
    public BusUser loginConfirm(@Param("username")String username, @Param("password")String password){
        return busUserService.loginConfirm(username, password);
    }
    @RequestMapping("/addBus")
    public BusUser addUser(@Param("username")String username, @Param("password") String password){
        BusUser busUser = new BusUser();
        busUser.setBus_user_password(password);
        busUser.setBus_user_name(username);
        String str = RandomStringUtils.randomAlphanumeric(5);
        busUser.setBus_user_name("用户"+str);
        return busUserService.save(busUser);
    }
    @RequestMapping("/resetInfo")
    public BusUser resetInfo(@Param("user_name")String user_name, @Param("user_tel")String user_tel, @Param("user_gender")String user_gender, @Param("user_mail")String user_mail, @Param("user_work")String user_work){

        BusUser busUser = busUserService.getOne(busUserService.findUserByName(user_tel).getBus_user_id());
        busUser.setBus_user_name(user_name);
        busUser.setBus_user_tel(user_tel);
        busUser.setBus_user_mail(user_mail);
        busUser.setBus_user_sex(user_gender);
        busUserService.save(busUser);
        return busUser;
    }
//    @RequestMapping("/findByName")
//    public BusUser findByName(@Param("user_name")String user_name){
//        System.out.println("用户名为: "+user_name);
//        return busUserService.findUserByName(user_name);
//    }
    @RequestMapping("/findByName")
    public BusUser findByName(@Param("user_name") String user_name) {
        System.out.println("用户名为: " + user_name);
        BusUser busUser = busUserService.findUserByName(user_name);
        System.out.println(busUser.getBus_user_name());
        System.out.println(busUser.getBus_user_id());
        if (busUser != null) {
            // 对返回的BusUser对象进行进一步操作
            String userName = busUser.getBus_user_name();
            System.out.println("用户名为1: " + userName);
            // 可根据需要构建响应实体，并将BusUser对象作为响应体返回
            return busUser;
        }else{
            System.out.println("失败");
            return busUser;
        }

    }
}
